package it.nard.controller;

import it.nard.listener.ControlDocumentListener;
import it.nard.persistence.dao.RprovolaMapper;
import it.nard.persistence.dao.RricottaMapper;
import it.nard.persistence.model.Rprovola;
import it.nard.persistence.model.RprovolaExample;
import it.nard.persistence.model.Rricotta;
import it.nard.persistence.model.RricottaExample;
import it.nard.sourceframes.MainFrame;
import it.nard.sourceframes.RRicottaPnl;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import org.apache.ibatis.session.SqlSession;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.decorator.HighlighterFactory;

public class RRicottaController  extends BasicController{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6729316031073309352L;



	public static final String R_RICOTTA = "ricotta";
	
	
	


	
	public RRicottaController(){
		super("Rintr. Ricotta", null);
	
	}
	
	
	
	
	public void select (MouseEvent mevent){
		
		try{
			this.selectedRow =  ((RRicottaPnl)controlPnl).getJTable0().convertRowIndexToModel( ((RRicottaPnl)controlPnl).getJTable0().getSelectedRow());
		}catch(Exception e ){
			
		}
		if(this.selectedRow==-1){
			return;
		}
		Rricotta rricotta = (Rricotta) objList.get(selectedRow);
		mainFrame.sendMessage("Selezionata Ricotta Prodotta.", MainFrame.OK_MESSAGE);
		
		this.fromBean(rricotta);
		
		
	}
	public void onDateSelect(){
		DefaultTableModel dtm = (DefaultTableModel) ((RRicottaPnl)controlPnl).getJTable0().getModel();
		dtm.getDataVector().removeAllElements();
		dtm.setNumRows(0);
		dtm.fireTableDataChanged();
		
		
		Integer dataStart = null;
		Integer dataStop = null;
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		
		try{
			String tempStart = sdf.format( ((RRicottaPnl)controlPnl).getJXDatePicker2().getDate());
			tempStart = tempStart.concat("01");
			
			dataStart = new Integer(tempStart);
			
			String tempStop = sdf.format( ((RRicottaPnl)controlPnl).getJXDatePicker2().getDate());
			tempStop = tempStop.concat("31");
			
			dataStop = new Integer(tempStop);
			
			
			
		}catch(Exception e ){
			e.printStackTrace();
		}
		
		RricottaExample rme = new RricottaExample();
		
		rme.createCriteria().andDataproduzioneBetween(dataStart, dataStop);
		
		
		SqlSession sqlSession = MainController.getSqlSessionFactory().openSession();
		
		objList = sqlSession.getMapper(RricottaMapper.class).selectByExample(rme);
		mainFrame.sendMessage("Selezionata Ricotta Prodotta.", MainFrame.OK_MESSAGE);
		
		sqlSession.close();
		
		dtm = (DefaultTableModel) ((RRicottaPnl)controlPnl).getJTable0().getModel();
		Iterator<Rricotta> iter = (Iterator<Rricotta>)objList.iterator();
		while(iter.hasNext()){
			Rricotta rRicotta = iter.next();
			String data4Table = "";
			try{
				SimpleDateFormat sdf4View = new SimpleDateFormat("dd/MM/yyyy");
				data4Table = sdf4View.format(SDF_4_DB.parse(rRicotta.getDataproduzione().toString()));
			}catch(Exception e ){
				
			}
			
			String totale = "";			
			Double totaleD = 0d;
	
			try{
				totaleD = Double.valueOf(rRicotta.getTotale());
				totale = totaleD.toString();
			}catch(Exception e ){} 
			
			dtm.addRow(new String[]{data4Table, totale});
		}
	
		
	
	}
	
	
	
	public void delete (){
		
		SqlSession sqlSession = MainController.getSqlSessionFactory().openSession();
		
		sqlSession.getMapper(RricottaMapper.class).deleteByPrimaryKey(((Rricotta)(objList.get(selectedRow))).getId());
	
		sqlSession.commit();
		
		mainFrame.sendMessage("Cancellata Ricotta Prodotta.", MainFrame.OK_MESSAGE);
		
		
		DefaultTableModel dtm = (DefaultTableModel) ((RRicottaPnl)controlPnl).getJTable0().getModel();
		try{
			dtm.removeRow(selectedRow);
		}catch(Exception e ){}
		
		
		clearPnl();
		sqlSession.close();
	}
	
	public void insert (){
		Rricotta rRicotta = new Rricotta();
		
		if(selectedRow!=-1 || ((RRicottaPnl)controlPnl).getJTable0().getModel().getRowCount()!=0){
			
			rRicotta = (Rricotta)this.objList.get(this.selectedRow);
		}
		
		Integer dataStart = null;
		Integer dataStop = null;
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		
		try{
			String tempStart = sdf.format( ((RRicottaPnl)controlPnl).getJXDatePicker2().getDate());
			tempStart = tempStart.concat("01");
			
			dataStart = new Integer(tempStart);
			
			String tempStop = sdf.format( ((RRicottaPnl)controlPnl).getJXDatePicker2().getDate());
			tempStop = tempStop.concat("31");
			
			dataStop = new Integer(tempStop);
			
			
			
		}catch(Exception e ){
			e.printStackTrace();
		}
		
		RricottaExample rme = new RricottaExample();
		
		rme.createCriteria().andDataproduzioneBetween(dataStart, dataStop);
		
		SqlSession sqlSession = MainController.getSqlSessionFactory().openSession();
		this.toBean(rRicotta);
		if(selectedRow==-1 || ((RRicottaPnl)controlPnl).getJTable0().getModel().getRowCount()==0){

					
					
					sqlSession.getMapper(RricottaMapper.class).insert(rRicotta);
				
					sqlSession.commit();
					
					mainFrame.sendMessage("Inserita Ricotta Prodotta.", MainFrame.OK_MESSAGE);
					
					
					
					objList = sqlSession.getMapper(RricottaMapper.class).selectByExample(rme);
					
					DefaultTableModel dtm = (DefaultTableModel)((RRicottaPnl)controlPnl).getJTable0().getModel();
					
					String data4Table = "";
					
					try{
						SimpleDateFormat sdf4View = new SimpleDateFormat("dd/MM/yyyy");
						data4Table = sdf4View.format(SDF_4_DB.parse(rRicotta.getDataproduzione().toString()));
					}catch(Exception e ){
						
					}
					
					String totale = "";			
					Double totaleD = 0d;
			
					try{
						totaleD = Double.valueOf(rRicotta.getTotale());
						totale = totaleD.toString();
					}catch(Exception e ){} 
					
					dtm.addRow(new String[]{data4Table, totale});

					clearPnl();
					
		
		}
		else{
					sqlSession.getMapper(RricottaMapper.class).updateByPrimaryKey(rRicotta);
				
					sqlSession.commit();
					mainFrame.sendMessage("Modificata Ricotta Prodotta.", MainFrame.OK_MESSAGE);
					
					
					objList = sqlSession.getMapper(RricottaMapper.class).selectByExample(rme);
					
					DefaultTableModel dtm = (DefaultTableModel)((RRicottaPnl)controlPnl).getJTable0().getModel();
					
					
					String data4Table = "";
					
					try{
						SimpleDateFormat sdf4View = new SimpleDateFormat("dd/MM/yyyy");
						data4Table = sdf4View.format(SDF_4_DB.parse(rRicotta.getDataproduzione().toString()));
					}catch(Exception e ){
						
					}
					
					String totale = "";
					
					Double totaleD = 0d;
			
					try{
						totaleD = Double.valueOf(rRicotta.getTotale());
						totale = totaleD.toString();
					}catch(Exception e ){} 
					
					dtm.setValueAt(data4Table,selectedRow,0);
					dtm.setValueAt(totale,selectedRow,1);

					clearPnl();
					
					
		}
		sqlSession.close();
	}
	
	public RRicottaPnl getMainPanel(){
		
			
		if( controlPnl==null){
			 controlPnl = new  RRicottaPnl(this);

		
	     ButtonGroup bg0 = new ButtonGroup();
	    
	     ButtonGroup bg1 = new ButtonGroup();
	     bg1.add(((RRicottaPnl)controlPnl).getJRadioButton2());
	     bg1.add(((RRicottaPnl)controlPnl).getJRadioButton3());
	     bg1.add(((RRicottaPnl)controlPnl).getJRadioButton4());
	     
	     
	     ButtonGroup bg2 = new ButtonGroup();
	     bg2.add(((RRicottaPnl)controlPnl).getJRadioButton5());
	     bg2.add(((RRicottaPnl)controlPnl).getJRadioButton6());
			 
	     JXTable theTable =  ((RRicottaPnl)controlPnl).getJTable0();
		 theTable.addHighlighter(HighlighterFactory.createAlternateStriping(Color.getHSBColor(229,229,229), Color.getHSBColor(208, 208, 208))); 
		
		 BasicController.tableDocumentListener.addTableListener( ((RRicottaPnl)controlPnl).getJTextField18(),  ((RRicottaPnl)controlPnl).getJTable0());
		 
		 theTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		 
		 ControlDocumentListener cdl = BasicController.controlDocumentListener;
		
		 ArrayList<JButton> listButton = new ArrayList<JButton>();
		 listButton.add( ((RRicottaPnl)controlPnl).getJButton2());
		 listButton.add( ((RRicottaPnl)controlPnl).getJButton4());
		
		 
		 ArrayList<JTextField> listMandatory = new ArrayList<JTextField>();
		
		 
		 ArrayList<JTextField> listNumeric = new ArrayList<JTextField>();
		 listNumeric.add(((RRicottaPnl)controlPnl).getJTextField2());
		 listNumeric.add(((RRicottaPnl)controlPnl).getJTextField9());
		 listNumeric.add(((RRicottaPnl)controlPnl).getJTextField10());
		 listNumeric.add(((RRicottaPnl)controlPnl).getJTextField12());
		 listNumeric.add(((RRicottaPnl)controlPnl).getJTextField13());
		 listNumeric.add(((RRicottaPnl)controlPnl).getJTextField16());
		 listNumeric.add(((RRicottaPnl)controlPnl).getJTextField17());
		
		
		 
		 
		 cdl.addControlElement(ControlDocumentListener.NUMBER_FIELD,  ((RRicottaPnl)controlPnl).getJTextField2(), listButton, listMandatory, listNumeric);
		 cdl.addControlElement(ControlDocumentListener.NUMBER_FIELD,  ((RRicottaPnl)controlPnl).getJTextField9(), listButton, listMandatory, listNumeric);
		 cdl.addControlElement(ControlDocumentListener.NUMBER_FIELD,  ((RRicottaPnl)controlPnl).getJTextField10(), listButton, listMandatory, listNumeric);
		 cdl.addControlElement(ControlDocumentListener.NUMBER_FIELD,  ((RRicottaPnl)controlPnl).getJTextField12(), listButton, listMandatory, listNumeric);
		 cdl.addControlElement(ControlDocumentListener.NUMBER_FIELD,  ((RRicottaPnl)controlPnl).getJTextField13(), listButton, listMandatory, listNumeric);
		 cdl.addControlElement(ControlDocumentListener.NUMBER_FIELD,  ((RRicottaPnl)controlPnl).getJTextField16(), listButton, listMandatory, listNumeric);
		 cdl.addControlElement(ControlDocumentListener.NUMBER_FIELD,  ((RRicottaPnl)controlPnl).getJTextField17(), listButton, listMandatory, listNumeric);
		 
		 
		}
		
		
		clearPnl();
		onDateSelect();
		return  ((RRicottaPnl)controlPnl);
	}

	@Override
	public List<? extends Object> getList4Print() {
		List<Rricotta> outList = new ArrayList<Rricotta>();
		if(selectedRow!=-1){
			outList.add((Rricotta)this.objList.get(selectedRow));
		}
		
		return outList;
	}

	@Override
	public String getControllerCode() {
		return R_RICOTTA;
	}
	
	
	public List<Rricotta> retriveListFromDates(Date dt1, Date dt2){
		List<Rricotta> outList = null;
		Integer dataStart = null;
		Integer dataStop = null;
		try{
			String tempStart = SDF_4_DB.format( dt1);
			
			dataStart = new Integer(tempStart);
			
			String tempStop = SDF_4_DB.format( dt2);
			
			dataStop = new Integer(tempStop);		
		}catch(Exception e ){
			e.printStackTrace();
		}
		
		RricottaExample rme = new RricottaExample();
		
		rme.createCriteria().andDataproduzioneBetween(dataStart, dataStop);
		rme.setOrderByClause("dataProduzione");
		
		SqlSession sqlSession = MainController.getSqlSessionFactory().openSession();
		
		outList = sqlSession.getMapper(RricottaMapper.class).selectByExample(rme);
		
		sqlSession.close();
		return outList;
	}
	

	@Override
	public void clearPnl() {
		RRicottaPnl rRicottaPnl  = (RRicottaPnl)controlPnl;
		
		rRicottaPnl.getJXDatePicker0().setDate(new Date());
		rRicottaPnl.getJXDatePicker1().setDate(new Date());
		rRicottaPnl.getJXDatePicker2().setDate(new Date());
		rRicottaPnl.getJXDatePicker3().setDate(new Date());
		
	
		
		rRicottaPnl.getJTextField2().setText("");
		rRicottaPnl.getJTextField9().setText("");
		rRicottaPnl.getJTextField10().setText("");
		rRicottaPnl.getJTextField12().setText("");
		rRicottaPnl.getJTextField13().setText("");
		rRicottaPnl.getJTextField16().setText("");
		rRicottaPnl.getJTextField17().setText("");
		
		
		
		rRicottaPnl.getJTextField18().setText("");
		
		
		
		rRicottaPnl.getJRadioButton3().setSelected(true);
		
		rRicottaPnl.getJRadioButton5().setSelected(true);
	
	}
	
	
	
	public void fromBean(Rricotta rricotta) {
		RRicottaPnl rRicottaPnl  = (RRicottaPnl)controlPnl;
		
		try{
			rRicottaPnl.getJXDatePicker0().setDate(SDF_4_DB.parse(Integer.valueOf(rricotta.getDataproduzione()).toString()));
		}catch(Exception e ){}
		
		try{
			rRicottaPnl.getJXDatePicker1().setDate(SDF_4_DB.parse(Integer.valueOf(rricotta.getScadenza()).toString()));
		}catch(Exception e ){}
		
		try{
			rRicottaPnl.getJXDatePicker3().setDate(SDF_4_DB.parse(Integer.valueOf(rricotta.getLavorazionelattedel()).toString()));
		}catch(Exception e ){}
	
		try{	
			rRicottaPnl.getJTextField2().setText(rricotta.getTotale().trim());
		}catch(Exception e ){}
		try{
			rRicottaPnl.getJTextField9().setText(rricotta.getAciditasieroin().trim());
		}catch(Exception e ){}
		try{
			rRicottaPnl.getJTextField10().setText(rricotta.getLottosale().trim());
		}catch(Exception e ){}
		try{
			rRicottaPnl.getJTextField12().setText(rricotta.getTempprecipitaz().trim());
		}catch(Exception e ){}
		try{
			rRicottaPnl.getJTextField13().setText(rricotta.getTempsostatempambiente().trim());
		}catch(Exception e ){}
		try{
			rRicottaPnl.getJTextField16().setText(rricotta.getStockcella().trim());
		}catch(Exception e ){}
		
		try{
			rRicottaPnl.getJTextField17().setText(rricotta.getLottoimballoprimario().trim());
		}catch(Exception e ){}


		
		
		rRicottaPnl.getJRadioButton2().setSelected(false);
		try{
			rRicottaPnl.getJRadioButton2().setSelected(rricotta.getBicchieri().booleanValue());
		}catch(Exception e ){}
		
		rRicottaPnl.getJRadioButton3().setSelected(false);
		try{
			rRicottaPnl.getJRadioButton3().setSelected(rricotta.getVaschetta().booleanValue());
		}catch(Exception e ){}
		
		rRicottaPnl.getJRadioButton4().setSelected(false);
		try{
			rRicottaPnl.getJRadioButton4().setSelected(rricotta.getCartapergamena().booleanValue());
		}catch(Exception e ){}
		
		rRicottaPnl.getJRadioButton5().setSelected(false);
		try{
			rRicottaPnl.getJRadioButton5().setSelected(rricotta.getPuntovendita().booleanValue());
		}catch(Exception e ){}
		
		rRicottaPnl.getJRadioButton6().setSelected(false);
		try{
			rRicottaPnl.getJRadioButton6().setSelected(rricotta.getVenditaesterna().booleanValue());
		}catch(Exception e ){}
		
		
		
	}
	
	
	
	public void toBean(Rricotta rricotta) {
		RRicottaPnl rRicottaPnl  = (RRicottaPnl)controlPnl;
		
		try{
			rricotta.setDataproduzione(Integer.valueOf(SDF_4_DB.format(rRicottaPnl.getJXDatePicker0().getDate())));
		}catch(Exception e ){}
		
		try{
			rricotta.setScadenza(Integer.valueOf(SDF_4_DB.format(rRicottaPnl.getJXDatePicker1().getDate())));
		}catch(Exception e ){}
		
		try{
			rricotta.setLavorazionelattedel(Integer.valueOf(SDF_4_DB.format(rRicottaPnl.getJXDatePicker3().getDate())));
		}catch(Exception e ){}
		
		
		try{
			rricotta.setTotale(rRicottaPnl.getJTextField2().getText().trim());
		}catch(Exception e ){}
		
		
		try{
			rricotta.setAciditasieroin(rRicottaPnl.getJTextField9().getText().trim());
		}catch(Exception e ){}
		
		try{
			rricotta.setLottosale(rRicottaPnl.getJTextField10().getText().trim());
		}catch(Exception e ){}
	
		try{
			rricotta.setTempprecipitaz(rRicottaPnl.getJTextField12().getText().trim());
		}catch(Exception e ){}
	
		try{
			rricotta.setTempsostatempambiente(rRicottaPnl.getJTextField13().getText().trim());
		}catch(Exception e ){}
		
		try{
			rricotta.setStockcella(rRicottaPnl.getJTextField16().getText().trim());
		}catch(Exception e ){}

		
		try{
			rricotta.setLottoimballoprimario(rRicottaPnl.getJTextField17().getText().trim());
		}catch(Exception e ){}
				

		
		try{
			rricotta.setBicchieri(rRicottaPnl.getJRadioButton2().isSelected());
		}catch(Exception e ){}
		
	
		try{
			rricotta.setVaschetta(rRicottaPnl.getJRadioButton3().isSelected());
		}catch(Exception e ){}
		
		try{
			rricotta.setCartapergamena(rRicottaPnl.getJRadioButton4().isSelected());
		}catch(Exception e ){}
		
	
		try{
			rricotta.setPuntovendita(rRicottaPnl.getJRadioButton5().isSelected());
		}catch(Exception e ){}
		
		
		try{
			rricotta.setVenditaesterna(rRicottaPnl.getJRadioButton6().isSelected());
		}catch(Exception e ){}
		
		
	
		
		
	}
	
	
	
	
	
}
